const WorkerPool = require('workerpool').pool;
const schedule = require('node-schedule');

// https://www.cnblogs.com/youxin/p/13513325.html js 任务调度
const scheduleCronstyle = () => {
    const id = "F70FCDA2-FF38-E7BE-2D88-17A27968C34E";
    // 写到 express 的后台任务中进行
    //周一至周日，每天下午5点31分执行一次
    schedule.scheduleJob(id, '0 05 11 * * 1-7', async () => {
        const pool = WorkerPool({ maxWorkers: 4 });

        const someTask = async (sum) => {
            await new Promise(resolve => setTimeout(resolve, 1000 * (Math.random() * 3)));
            console.log("内容: " + sum)

            return sum * 2;
        }

        for (let i = 0; i < 10; i++) {
            pool.exec(someTask, [i])
                .then(result => {
                    // console.log(result)
                    // 处理结果
                });
        }

        console.log('scheduleCronstyle:' + new Date());
    });

    console.log("schedule.scheduledJobs[id]", schedule.scheduledJobs[id])
    
    schedule.cancelJob(id)
    console.log("schedule.scheduledJobs[id]", schedule.scheduledJobs[id])

    schedule.scheduledJobs["123"]?.cancel() // 如果没有 id 则返回 undefined
    console.log(schedule.scheduledJobs[id]?.cancel())

    // console.log(schedule.scheduledJobs)
    // setTimeout(() => {
    //     schedule.cancelJob(id);
    //     console.log("取消执行任务")
    // }, 2000)
}

scheduleCronstyle();


